from typing import Optional

from tortoise.expressions import Q

from app.core.crud import CRUDBase
from app.models.rotor import RotorType
from app.schemas.rotor import RotorTypeCreate, RotorTypeUpdate


class RotorTypeController(CRUDBase[RotorType, RotorTypeCreate, RotorTypeUpdate]):
    def __init__(self):
        super().__init__(model=RotorType)

    async def get_rotor_types(
        self, *, page: int = 1, page_size: int = 10, name: Optional[str] = None
    ):
        """获取旋翼类型列表"""
        query = Q()
        if name:
            query &= Q(name__contains=name)
        skip = (page - 1) * page_size
        data = await self.model.filter(query).offset(skip).limit(page_size)
        total = await self.model.filter(query).count()
        return {"items": data, "total": total}

    async def is_exists(self, name: str) -> bool:
        """检查旋翼类型名称是否已存在"""
        return await self.model.filter(name=name).exists()


rotor_type_controller = RotorTypeController() 